<?php
    define('CONFIG_FILE',true);
    include '../config.php';
    
    define('DB_FILE',true);
    require_once '../Tool/DB/SqlDBManager.class.php';
    require_once '../Tool/Common/PrepareInput.php';

    session_start();
    if (empty($_SESSION["userType"])) {
        $url="../Index.php";
        include_once '../Tool/Common/Redirect.php';
        exit();
    }

    if($_SESSION["userType"]!="System Admin"){
        $url="../Index.php";
        include_once '../Tool/Common/Redirect.php';
        exit();;
    }
    
    $UpdateCentreId=$UpdateCentreName=$UpdateAddress=$UpdatePostalCode="";  

    if(isset($_REQUEST['UpdateCentreId'])){ 
        $UpdateCentreId=prepareInput($_REQUEST['UpdateCentreId']); 
    }
    if(isset($_REQUEST['UpdateCentreName'])){ 
        $UpdateCentreName=prepareInput($_REQUEST['UpdateCentreName']); 
    }
    if(isset($_REQUEST['UpdateAddress'])) { 
        $UpdateAddress=prepareInput($_REQUEST['UpdateAddress']); 
    }
    if(isset($_REQUEST['UpdatePostalCode'])) { 
        $UpdatePostalCode=prepareInput($_REQUEST['UpdatePostalCode']); 
    }

    $result=array();

    $result['flagCentreId']=0;
    $result['flagCentreName']=0;
    $result['flagCentreNameExist']=1;
    $result['flagAddress']=0;  
    $result['flagPostalCode']=0;

    if(preg_match("/^[0-9]*$/",$UpdateCentreId)&&!empty($UpdateCentreId)){
        $result['flagCentreId']=1;
    }
    if(preg_match("/^[0-9A-Za-z() ]*$/",$UpdateCentreName)&&!empty($UpdateCentreName)){
        $result['flagCentreName']=1;
    }

    //Check centreName exist or not 
    $sqlDBManager = new SqlDBManager();
    
    $sql="SELECT CentreName FROM Centre WHERE CentreId=?";
    $parameters=array($UpdateCentreId);
    $res=$sqlDBManager->queryRow($sql,$parameters);

    if(!empty($res)){
        $existedCentreName=$res['CentreName'];
        if($existedCentreName==$UpdateCentreName){
            $result['flagCentreNameExist']=1;  
        }else{
            $sql="Select CentreName FROM Centre";
            $res=$sqlDBManager->queryRows($sql);
            if(!empty($res)){
                for($i=0;$i<count($res);$i++){
                    $result['flagCentreNameExist']=0;
                    if($res[$i]['CentreName']==$UpdateCentreName){
                        break;
                    }else{
                        $result['flagCentreNameExist']=1; 
                    }
                }
            }   
        }   
    }else{
        $sql="Select CentreName FROM Centre";
        $res=$sqlDBManager->queryRows($sql);
        if(!empty($res)){
            for($i=0;$i<count($res);$i++){
                $result['flagCentreNameExist']=0;
                if($res[$i]['CentreName']==$UpdateCentreName){
                    break;
                }else{
                    $result['flagCentreNameExist']=1; 
                }
            }
        }     
    }  

    if(preg_match("/^[0-9A-Za-z#-:() ]*$/",$UpdateAddress)&&!empty($UpdateAddress)){
        $result['flagAddress']=1;
    }
    if(preg_match("/^[0-9]{6}$/",$UpdatePostalCode)&&!empty($UpdatePostalCode)){
        $result['flagPostalCode']=1;
    }

    echo json_encode($result);
?>